﻿using LanguagePolish.EntityModel;
using LanguagePolish.ViewModels;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Xceed.Wpf.Toolkit;

namespace LanguagePolish.Views
{
    /// <summary>
    /// Логика взаимодействия для AddLessonView.xaml
    /// </summary>
    public partial class AddLessonView : Window
    {
        private readonly AddLessonViewModel vm;

        public AddLessonView(Lesson lesson, bool isEdit = false)
        {
            InitializeComponent();
            vm = new AddLessonViewModel(lesson, isEdit);
            DataContext = vm; 
            UpdateOkButtonState();

        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
        }

        private void TextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            WpfHelper.ForcedValidation(Root);
            UpdateOkButtonState();
        }

        private void ValueChanged(object sender, RoutedEventArgs e)
        {
            if (From != null && Till != null && btnOk != null)
            {
                if (From.Value > Till.Value)
                    btnOk.IsEnabled = false;
                else
                    btnOk.IsEnabled = true;
            }

        }

        private void UpdateOkButtonState()
        {
            // активация кнопки ОК в зависимости от валидности данных
            btnOk.IsEnabled = !vm.HasErrors;
        }

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            WpfHelper.ForcedValidation(Root);
        }

    }
}
